#pragma once
#include <iostream>
#include <string>
#include "inetAddr.hpp"
#include"Log.hpp"
#include<set>
#include <list>
#include<memory>

#include<sys/types.h>
#include<sys/socket.h>
using namespace LogMudule;
class userInterFace
{
public:
    virtual ~userInterFace() = default;
    virtual void sendTo(int sockfd, const std::string &message) = 0;
    virtual bool operator == (const inetAddr& u) = 0;
};

class user : public userInterFace
{
public:
    user(inetAddr &id): _id(id)
    {
    }
    void sendTo(int sockfd, const std::string &message) override
    {
        LOG(LogLevel::DEBUG) << "send message to " << _id.addr() <<  "info" << ":" << message;

        int  n = ::sendto(sockfd, message.c_str(), message.size(), 0, _id.netAdddr(), _id.netAddrLen());
        (void)n;
    }
    ~user()
    {
    }
    bool operator == (const inetAddr& u)override
    {
        return _id == u;
    }

private:
    inetAddr _id;
};

// zdl:: 对用户进行管理
// zdl:: 对所有的用户进行管理！！！

// zdl:: observe mode
class userManager
{

public:
    userManager()
    {

    }
    bool addUser(inetAddr& id)
    {
        
        for (auto& u : _online_user)
        {
            if (*u == id)
            {
                LOG(LogLevel::INFO) << (id.addr()) << "用户已存在";
                return false;
            }
        }
        LOG(LogLevel::INFO) << "新增该用户" << id.addr() ;
        _online_user.emplace(std::make_shared<user>(id));
        return true;
    }
    void deleteUser(const inetAddr& usr)
    {

    }
    
    void  router(int sockfd, const std::string& message)
    {
        for (auto& u : _online_user)
        {
            u->sendTo(sockfd, message);
        }
    }

private:
    std::set<std::shared_ptr<user>> _online_user;
    
};
